<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="78px">
      <el-form-item label="课题编号" prop="projectNumber">
        <el-input
          v-model="queryParams.projectNumber"
          placeholder="请输入课题编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="课题名称" prop="topicName">
        <el-input
          v-model="queryParams.topicName"
          placeholder="请输入课题名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="课题状态" prop="projectState">
        <el-select v-model="queryParams.projectState" placeholder="请选择课题状态" clearable>
          <el-option
            v-for="dict in dict.type.viillage_project_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['village:topics:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['village:topics:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['village:topics:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['village:topics:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="topicsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="编号" align="center">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="课题编号" align="center" prop="projectNumber"/>
      <el-table-column label="课题名称" align="center" prop="topicName"/>
      <el-table-column label="开课时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="课题负责人" align="center" prop="speaker"/>
      <el-table-column label="课题领域" align="center" prop="projectDomain"/>
      <el-table-column label="资金预算" align="center" prop="capitalBudget"/>
      <el-table-column label="课题状态" align="center" prop="projectState">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.viillage_project_state" :value="scope.row.projectState"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="skipDetail(scope.row)"
                     v-hasPermi="['village:pedagogical:edit']"
          >详情
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['village:topics:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['village:topics:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改横向课题管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="850px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" :inline="true" label-width="140px">

        <el-form-item label="课题名称" prop="topicName">
          <el-input v-model="form.topicName" placeholder="请输入课题名称"/>
        </el-form-item>
        <el-form-item label="课题编号" prop="projectNumber">
          <el-input v-model="form.projectNumber" placeholder="请输入课题编号"/>
        </el-form-item>

        <el-form-item label="课题负责人" prop="speaker">
          <el-input v-model="form.speaker" placeholder="请输入课题负责人"/>
        </el-form-item>
        <el-form-item label="开课单位" prop="organizingUnit">
          <el-input v-model="form.organizingUnit" placeholder="请输入开课单位"/>
        </el-form-item>
        <el-form-item label="开课时间" prop="startTime">
          <el-date-picker clearable
                          v-model="form.startTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择开课时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker clearable
                          v-model="form.endTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择结束时间"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item label="课题成员" prop="projectMember">
          <el-input v-model="form.projectMember" placeholder="请输入课题成员"/>
        </el-form-item>

        <el-form-item label="研究领域" prop="projectDomain">
          <el-input v-model="form.projectDomain" placeholder="请输入课题领域"/>
        </el-form-item>
        <el-form-item label="资金预算" prop="capitalBudget">
          <el-input v-model="form.capitalBudget" placeholder="请输入资金预算"/>
        </el-form-item>
        <el-form-item label="资金来源" prop="capitalSource">
          <el-input v-model="form.capitalSource" placeholder="请输入资金来源"/>
        </el-form-item>
        <el-form-item label="课题状态" prop="projectState">
          <el-radio-group v-model="start" >
            <el-radio
              v-for="dict in dict.type.viillage_project_state"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="课题简介" prop="introduction">
          <el-input style="width: 600px" v-model="form.introduction" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="资料上传列表：" prop="cutOffTime">
          <el-button type="primary" @click="dataopen = true">上传资料</el-button>
        </el-form-item>
        <el-dialog title="资料上传" :visible.sync="dataopen" width="900px" append-to-body>
          <!--          <el-form-item style="width: 800px" label="相关内容附件" prop="projectAccessory">-->
          <!--            <file-upload :isShowTip="false" v-model="form.projectAccessory"/>-->
          <!--          </el-form-item>-->
          <el-form-item label="申报书" prop="declaration">
            <file-upload style="width: 500px;margin-left: 16%" :isShowTip="false"
                         v-model="form.declaration"
            />
          </el-form-item>
          <el-form-item label="任务书" prop="assignmentBook">
            <file-upload style="width: 500px;margin-left: 16%" :isShowTip="false"
                         v-model="form.assignmentBook"
            />
          </el-form-item>
          <el-form-item label="开题报告" prop="thesisProposal">
            <file-upload style="width: 500px;margin-left: 16%" :isShowTip="false"
                         v-model="form.thesisProposal"
            />
          </el-form-item>
          <el-form-item label="中期报告" prop="interimReport">
            <file-upload style="width: 500px;margin-left: 16%" :isShowTip="false"
                         v-model="form.interimReport"
            />
          </el-form-item>
          <el-form-item label="结题材料" prop="concludingMaterial">
            <file-upload style="width: 500px;margin-left: 16%" :isShowTip="false"
                         v-model="form.concludingMaterial"
            />
          </el-form-item>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="dataopen = false">确 定</el-button>
          </div>
        </el-dialog>

        <el-form-item style="width: 100%" label="备注" prop="subjectSummary">
          <el-input style="width: 600px" v-model="form.subjectSummary" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="详细信息" :visible.sync="detailopen" width="800px" append-to-body>
      <el-form ref="form" :model="form" :inline="true" label-width="160px">

        <el-form-item style="width: 300px" label="课题名称：" prop="topicName">
          {{ form.topicName }}
        </el-form-item>
        <el-form-item label="课题编号：" prop="projectNumber">
          {{ form.projectNumber }}
        </el-form-item>

        <el-form-item style="width: 300px" label="课题负责人：" prop="speaker">
          {{ form.speaker }}
        </el-form-item>
        <el-form-item label="开课单位：" prop="organizingUnit">
          {{ form.speaker }}
        </el-form-item>
        <el-form-item style="width: 300px" label="开课时间：" prop="startTime">
          {{ form.startTime }}
        </el-form-item>
        <el-form-item label="结束时间：" prop="endTime">
          {{ form.startTime }}
        </el-form-item>

        <el-form-item style="width: 300px" label="课题成员：" prop="projectMember">
          {{ form.projectMember }}
        </el-form-item>

        <el-form-item label="课题领域：" prop="projectDomain">
          {{ form.projectDomain }}
        </el-form-item>
        <el-form-item style="width: 300px" label="资金预算：" prop="capitalBudget">
          {{ form.capitalBudget }}
        </el-form-item>
        <el-form-item label="资金来源：" prop="capitalSource">
          {{ form.capitalSource }}
        </el-form-item>
        <el-form-item style="width: 700px" label="课题状态：" prop="projectState">
          <dict-tag :options="dict.type.viillage_project_state" :value="form.projectState"/>
        </el-form-item>
        <el-form-item style="width: 100%" label="课题简介：" prop="introduction">
          <div style="width: 500px;">
            {{ form.introduction }}
          </div>
        </el-form-item>

        <el-form-item label="查看资料列表：" prop="cutOffTime">
          <el-button type="primary" @click="dataopen = true">查看资料列表</el-button>
        </el-form-item>
        <el-dialog title="资料列表" :visible.sync="dataopen" width="900px" append-to-body>

          <el-form-item style="width: 600px" label="申报书：" prop="declaration">
            <file-upload style="margin-left: 240px;" :openUplad="false" :isShowTip="false" v-model="form.declaration"/>
          </el-form-item>
          <el-form-item style="width: 600px" label="任务书：" prop="assignmentBook">
            <file-upload style="margin-left: 240px;" :openUplad="false" :isShowTip="false"
                         v-model="form.assignmentBook"
            />
          </el-form-item>
          <el-form-item style="width: 600px" label="开题报告：" prop="thesisProposal">
            <file-upload style="margin-left: 240px;" :openUplad="false" :isShowTip="false"
                         v-model="form.thesisProposal"
            />
          </el-form-item>
          <el-form-item style="width: 600px" label="中期报告：" prop="interimReport">
            <file-upload style="margin-left: 240px;" :openUplad="false" :isShowTip="false" v-model="form.interimReport"/>
          </el-form-item>
          <el-form-item style="width: 600px" label="结题材料：" prop="concludingMaterial">
            <file-upload style="margin-left: 240px;" :openUplad="false" :isShowTip="false"
                         v-model="form.concludingMaterial"
            />
          </el-form-item>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="dataopen = false">确 定</el-button>
          </div>
        </el-dialog>

        <el-form-item style="width: 100%" label="备注：" prop="subjectSummary">
          <div style="width: 500px;">
            {{ form.subjectSummary }}
          </div>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { listTopics, getTopics, delTopics, addTopics, updateTopics } from '@/api/village/topics'

export default {
  name: 'Topics',
  dicts: ['viillage_project_state'],
  data() {
    return {
      start: 1,
      dataopen: false,
      detailopen: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 横向课题管理表格数据
      topicsList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectNumber: null,
        topicName: null,
        projectState: null
      },
      // 表单参数
      form: {
        projectState: 1
      },
      // 表单校验
      rules: {
        topicName: [
          { required: true, message: '课题名称不能为空', trigger: 'blur' }
        ],
        startTime: [
          { required: true, message: '开课时间不能为空', trigger: 'blur' }
        ],
        endTime: [
          { required: true, message: '结束时间不能为空', trigger: 'blur' }
        ],
        speaker: [
          { required: true, message: '课题负责人不能为空', trigger: 'blur' }
        ],
        capitalBudget:[
          { required: true, message: '课题负责人不能为空', trigger: 'blur' },
          {
            pattern: /^\d+(\.\d+)?$/, // 正则表达式，匹配一个或多个数字，后面可跟一个小数点和一个或多个数字
            message: '请输入数字',
            trigger: ['input', 'blur']
          }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    skipDetail(row) {
      this.reset()
      const id = row.id || this.ids
      getTopics(id).then(response => {
        this.form = response.data
        this.detailopen = true

      })
    },
    /** 查询横向课题管理列表 */
    getList() {
      this.loading = true
      listTopics(this.queryParams).then(response => {
        this.topicsList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    detalicancel() {
      this.detailopen = false
      this.reset()
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        projectNumber: null,
        topicName: null,
        organizingUnit: null,
        startTime: null,
        endTime: null,
        speaker: null,
        projectMember: null,
        introduction: null,
        projectDomain: null,
        capitalBudget: null,
        capitalSource: null,
        projectAccessory: null,
        projectState: null,
        subjectSummary: null,
        participants: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
        isDelete: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加横向课题管理'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getTopics(id).then(response => {
        this.form = response.data
        this.start = this.form.projectState
        this.open = true
        this.title = '修改横向课题管理'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        this.form.projectState = this.start
        if (valid) {
          if (this.form.id != null) {
            updateTopics(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addTopics(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除横向课题名称为"' + row.topicName + '"的数据项？').then(function() {
        return delTopics(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('village/topics/export', {
        ...this.queryParams
      }, `topics_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
